package com.yyfax.pay.order.data.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.order.data.dao.AuthOrderDao;
import com.yyfax.pay.order.data.service.AuthOrderService;
import com.yyfax.pay.order.model.po.AuthOrderPO;

/**
 * 独立鉴权订单服务实现
 *
 * @author buyi
 * @date 2017-04-12 11:09:15
 * @since 1.0.0
 */
@Service
public class AuthOrderServiceImpl extends BaseServiceImpl implements AuthOrderService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	private final String AUTH_ORDER_ID_PREFIX = "A";
	private final String AUTH_ORDER_ID_CONCAT = "_";

	@Autowired
	private AuthOrderDao authOrderDao;

	public AuthOrderServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}

	/**
	 * 获取支付订单业务主键
	 * 
	 * @author buyi
	 * @date 2017-04-15 14:01:56
	 * @since v1.0.0
	 * @param merchantCode
	 * @param orderId
	 * @return
	 */
	private String getAuthOrderId(String merchantCode, String orderId) {
		return AUTH_ORDER_ID_PREFIX.concat(AUTH_ORDER_ID_CONCAT).concat(merchantCode).concat(AUTH_ORDER_ID_CONCAT).concat(orderId);
	}

	@Override
	public AuthOrderPO queryByIdAndMerchantCode(int id, String merchantCode) throws Exception {
		if (id <= 0 && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 'id' 不能为0，且 'merchantCode' 不能为空 ");
			return null;
		}

		return authOrderDao.selectByIdAndMerchantCode(id, merchantCode);
	}

	@Override
	public int save(AuthOrderPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空，返回-1");
			return 0;
		}

		return authOrderDao.insert(po);
	}

	@Override
	public AuthOrderPO queryByOrderIdAndMerchantCode(String orderId, String merchantCode) {
		if (StringUtils.isBlank(orderId) && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 orderId/merchantCode 为空 ");
			return null;
		}

		return authOrderDao.selectByOrderIdAndMerchantCode(orderId, merchantCode);
	}

	@Override
	public String saveAndReturnBusiKey(AuthOrderPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空");
			return null;
		}

		String authOrderId = getAuthOrderId(po.getMerchantCode(), po.getOrderId());// 根据策略生成业务主键

		po.setAuthOrderId(authOrderId);
		po.setCreateTime(DateUtil.getCurrentDateTime());

		authOrderDao.insert(po);

		return authOrderId;
	}

	@Override
	public int modify(AuthOrderPO authOrderPO) throws Exception {
		if (authOrderPO == null || authOrderPO.getId() == null) {
			return -1;
		}
		return authOrderDao.update(authOrderPO);
	}

	@Override
	public int modifyState(AuthOrderPO authOrderPO) throws Exception {
		if (authOrderPO == null) {
			return -1;
		}

		return authOrderDao.updateState(authOrderPO);
	}

}
